//
//  ProdDetailHeaderView.m
//  JiaoX
//
//  Created by yinqy on 15/1/12.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ProdDetailHeaderView.h"
#import "PriceBarView.h"
#import "SGFocusImageItem.h"
#import "PromotionView.h"
#import "PromiseLabel.h"
#import "SpePropertyView.h"

#define PROD_TITLE_FONT_SIZE [DimensUtils getScaleX:15]
#define SELLPOINT_LABEL_FONT_SIZE [DimensUtils getScaleX:13]

@interface ProdDetailHeaderView()
{

    UILabel *_nameLabel;
    PriceBarView *_priceBarView;
    
    UILabel *_sellPointLabel;
    
    SpePropertyView *_speView;
    
}
@property(nonatomic,strong)PromotionView *promotionView;
@end
@implementation ProdDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self setUserInteractionEnabled:YES];
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setFont:[UIFont systemFontOfSize:PROD_TITLE_FONT_SIZE]];
        [_nameLabel setTextColor:[UIColor blackColor]];
        
        [self addSubview:_nameLabel];
        
        _sellPointLabel = [[UILabel alloc]init];
        [_sellPointLabel setTextColor:JXColor(102, 102, 102)];
        [_sellPointLabel setFont:[UIFont systemFontOfSize:SELLPOINT_LABEL_FONT_SIZE]];
        
        [self addSubview:_sellPointLabel];
        
    }
    return self;
}

-(void)setProdDetail:(ProductDetail *)prodDetail{
    _prodDetail = prodDetail;
    [self setBannerData:prodDetail.prodImages];
    CGSize prodNameSize = [prodDetail.prodName sizeWithFont:[UIFont systemFontOfSize:PROD_TITLE_FONT_SIZE] constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
    [_nameLabel setNumberOfLines:0];
    [_nameLabel setText:prodDetail.prodName];
    [_nameLabel setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_bannerView.frame)+15, JXScreenW-JXPublicInset*2, prodNameSize.height)];
    
    //价格
    _priceBarView = [[PriceBarView alloc]initWithNowPrice:[NSString stringWithFormat:@"%.0f",prodDetail.prodNewPrice] oldPrice:[NSString stringWithFormat:@"%.0f",prodDetail.prodOldPrice] buyCount:[NSString stringWithFormat:@"%d",prodDetail.prodBuyCount]];
    [_priceBarView setX:0];
    [_priceBarView setY:CGRectGetMaxY(_nameLabel.frame)+10];
    [self addSubview:_priceBarView];
    
    //赠品的view
    _promotionView = [[PromotionView alloc]init];
    [_promotionView setPromotion:prodDetail.prodPromotion];
    _promotionView.x = 10;
    _promotionView.y = CGRectGetMaxY(_priceBarView.frame)+(prodDetail.prodPromotion==nil?0:10);
    [self addSubview:_promotionView];

    
    CGFloat inset = 15;
    if ([Utils isBlankString:prodDetail.prodSellingPoint]) {
        inset = 0;
    }
    //卖点
    CGSize sellPointSize = [prodDetail.prodSellingPoint sizeWithFont:[UIFont systemFontOfSize:SELLPOINT_LABEL_FONT_SIZE] constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
    [_sellPointLabel setNumberOfLines:0];
    [_sellPointLabel setText:prodDetail.prodSellingPoint];
    [_sellPointLabel setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_promotionView.frame)+inset, JXScreenW-JXPublicInset*2, inset==0?0:sellPointSize.height)];
    
    CGFloat height = CGRectGetMaxY(_sellPointLabel.frame);
    
    for (int i=0; i<prodDetail.prodSecurity.count; i++) {
        PromiseLabel *label = [[PromiseLabel alloc]initWithLabelText:prodDetail.prodSecurity[i]];
        
        switch (i) {
            case 0:
                height = height + 30 + label.height;
                label.x = JXPublicInset;
                break;
            case 1:
                label.x = JXScreenW/2-label.width/2;
                break;
            case 2:
                label.x = JXScreenW-JXPublicInset-label.width;
                break;
            default:
                break;
        }
        label.y = CGRectGetMaxY(_sellPointLabel.frame)+15;
        [self addSubview:label];
        
    }
    
    _speView = [[SpePropertyView alloc]initWithArray:prodDetail.prodSpeProperty];
    _speView.x = 0;
    _speView.y = height;
    [self addSubview:_speView];
    height = CGRectGetMaxY(_speView.frame);
    
    UIView *splite = [[UIView alloc]init];
    [splite setFrame:CGRectMake(JXPublicInset, height, JXScreenW-JXPublicInset, 0.5)];
    [splite setBackgroundColor:SEPARATORCOLOR];
    [self addSubview:splite];

    [self setFrame:CGRectMake(0, 0, JXScreenW, height+0.5)];
}


//设置商品头图
-(void)setBannerData:(NSArray *)bannerArray{
    if(!bannerArray.count)return;
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:bannerArray.count+2];
    
    for (int i = 0; i < bannerArray.count; i++)
    {
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:bannerArray[i] image:bannerArray[i] tag:i];
        [itemArray addObject:item];
    }
    
    if(bannerArray.count>1){
        SGFocusImageItem *startItem = [[SGFocusImageItem alloc] initWithTitle:bannerArray[bannerArray.count-1] image:bannerArray[bannerArray.count-1] tag:bannerArray.count-1];
        [itemArray insertObject:startItem atIndex:0];
        
        SGFocusImageItem *endItem = [[SGFocusImageItem alloc] initWithTitle:bannerArray[0] image:bannerArray[0] tag:bannerArray.count-1];
        [itemArray addObject:endItem];
    }
    
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, JXScreenW, JXScreenW) delegate:nil imageItems:itemArray isAuto:NO];
    [bannerView scrollToIndex:0];
    _bannerView = bannerView;
    [self addSubview:_bannerView];
}

- (void)dealloc
{
    [_promotionView invalidateTimer];
}

@end
